package cn.wmyskxz.pojo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 闪电行者
 * @create 2023/6/29 - 22:46
 */
import java.util.Scanner;

    public class Main {
        // 定义学生类
        static class Student {
            String id; // 学号
            String name; // 姓名
            String college; // 所属学院
            String major; // 专业
            String grade; // 年级
            String clazz; // 班级
            String dorm; // 宿舍

            // 构造函数
            public Student(String id, String name, String college, String major, String grade, String clazz, String dorm) {
                this.id = id;
                this.name = name;
                this.college = college;
                this.major = major;
                this.grade = grade;
                this.clazz = clazz;
                this.dorm = dorm;
            }

            // 重写toString方法，方便输出学生信息
            @Override
            public String toString() {
                return id + "," + name + "," + college + "," + major + "," + grade + "," + clazz + "," + dorm;
            }
        }

        // 定义缴费信息类
        static class Payment {
            String id; // 学号
            String semester; // 学期
            double tuition; // 学费
            double housing; // 住宿费

            // 构造函数
            public Payment(String id, String semester, double tuition, double housing) {
                this.id = id;
                this.semester = semester;
                this.tuition = tuition;
                this.housing = housing;
            }

            // 重写toString方法，方便输出缴费信息
            @Override
            public String toString() {
                return id + "," + semester + "," + tuition + "," + housing;
            }
        }

        public static void main(String[] args) throws IOException, InterruptedException {
            // 定义注册信息和缴费信息的文件名
            String studentFile = "src/main/java/cn/wmyskxz/pojo/cstudents.txt";
            String paymentFile = "src/main/java/cn/wmyskxz/pojo/payments.txt";

            // 读取注册信息和缴费信息，存储到列表中
            List<Student> students = readStudents(studentFile);
            List<Payment> payments = readPayments(paymentFile);

            // 定义学院为key，学费收缴总额为value的映射表
            Map<String, Double> tuitionMap = new HashMap<>();
            // 定义学院为key，住宿费收缴总额为value的映射表
            Map<String, Double> housingMap = new HashMap<>();

            // 创建学生线程
            Thread studentThread = new Thread(() -> {
                Scanner scanner = new Scanner(System.in);
                // 循环读取学生信息
                while (true) {
                    System.out.println("请输入学生信息（学号,姓名,学院,专业,年级,班,宿舍），输入q退出：");
                    String input = scanner.nextLine();
                    if ("q".equalsIgnoreCase(input)) break;
                    String[] parts = input.split(",");
                    if (parts.length != 7) {
                        System.out.println("输入格式不正确！");
                        continue;
                    }
                    Student student = new Student(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
                    students.add(student); // 添加学生信息到列表中
                    System.out.println("学生信息添加成功！");
                    // 循环读取缴费信息
                    while (true) {
                        System.out.println("请输入缴费信息（学期,学费,住宿费），输入q退出：");
                        input = scanner.nextLine();
                        if ("q".equalsIgnoreCase(input)) break;
                        parts = input.split(",");
                        if (parts.length != 3) {
                            System.out.println("输入格式不正确！");
                            continue;
                        }
                        Payment payment = new Payment(student.id, parts[0], Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
                        payments.add(payment); // 添加缴费信息到列表中
                        System.out.println("缴费信息添加成功！");
                    }
                }
                scanner.close();
            });

            // 创建老师线程
            Thread teacherThread = new Thread(() -> {
                // 统计学费收缴总额和住宿费收缴总额
                for (Payment payment: payments) {
                    String college = getCollegeById(students, payment.id); // 根据学号获取学院信息
                    double tuition = payment.tuition;
                    double housing = payment.housing;
                    // 如果学院信息已存在于映射表中，则更新该学院的收缴总额
                    if (tuitionMap.containsKey(college)) {
                        tuition += tuitionMap.get(college);
                    }
                    tuitionMap.put(college, tuition);
                    if (housingMap.containsKey(college)) {
                        housing += housingMap.get(college);
                    }
                    housingMap.put(college, housing);
                }
                // 输出学院的学费收缴总额和住宿费收缴总额
                System.out.println("各学院学费收缴情况：");
                for (String college : tuitionMap.keySet()) {
                    System.out.println(college + ":" + tuitionMap.get(college));
                }
                System.out.println("各学院住宿费收缴情况：");
                for (String college : housingMap.keySet()) {
                    System.out.println(college + ":" + housingMap.get(college));
                }
            });

            // 启动学生线程和老师线程
            studentThread.start();
            teacherThread.start();

            // 等待学生线程和老师线程执行完毕
            studentThread.join();
            teacherThread.join();

            // 将注册信息和缴费信息写入文件中
            writeStudents(studentFile, students);
            writePayments(paymentFile, payments);
        }

        /**
         * 根据学号获取学生所在学院
         */
        private static String getCollegeById(List<Student> students, String id) {
            for (Student student : students) {
                if (student.id.equals(id)) {
                    return student.college;
                }
            }
            return null;
        }

        /**
         * 从文件中读取注册信息
         */
        private static List<Student> readStudents(String fileName) throws IOException {
            List<Student> students = new ArrayList<>();
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                Student student = new Student(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
                students.add(student);
            }
            reader.close();
            return students;
        }

        /**
         * 将注册信息写入文件中
         */
        private static void writeStudents(String fileName, List<Student> students) throws IOException {
            FileWriter writer = new FileWriter(fileName);
            for (Student student : students) {
                writer.write(student.toString() + "\n");
            }
            writer.close();
        }

        /**
         * 从文件中读取缴费信息
         */
        private static List<Payment> readPayments(String fileName) throws IOException {
            List<Payment> payments = new ArrayList<>();
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                Payment payment = new Payment(parts[0], parts[1], Double.parseDouble(parts[2]), Double.parseDouble(parts[3]));
                payments.add(payment);
            }
            reader.close();
            return payments;
        }

        /**
         * 将缴费信息写入文件中
         */
        private static void writePayments(String fileName, List<Payment> payments) throws IOException {
            FileWriter writer = new FileWriter(fileName);
            for (Payment payment : payments) {
                writer.write(payment.toString() + "\n");
            }
            writer.close();
        }

}
